package com.axend.aerosense.room.ui.adapter;


import android.content.Context;
import android.view.View;

import com.axend.aerosense.common.util.StringLengthUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.axend.aerosense.common.bean.RadarBean;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomLayoutDeviceItemBinding;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ChenHongWei
 * @date :2021/1/5 16:36
 */

public class RoomDeviceListAdapter
        extends BaseQuickAdapter<RadarBean, BaseDataBindingHolder<RoomLayoutDeviceItemBinding>> {

    private String roomName;
    private boolean isUnassigned = false;  //是否为为分配模式
    private boolean isShowCheckBox = false;  //是否显示选择框
    private boolean isSelectAll = false;  //是否全选
    private Map<Integer, RadarBean> checkedItems = new HashMap<>();  //已选中项
    private OnSelect onSelect;

    public RoomDeviceListAdapter(String roomName) {
        super(R.layout.room_layout_device_item);
        this.roomName = roomName;
    }

    public RoomDeviceListAdapter(boolean isUnassigned){
        super(R.layout.room_layout_device_item);
        this.isUnassigned = isUnassigned;
    }

    @Override
    protected void convert(@NotNull BaseDataBindingHolder<RoomLayoutDeviceItemBinding> baseViewHolder, RadarBean radarBean) {
        if(null == radarBean){
            return;
        }
        RoomLayoutDeviceItemBinding binding = baseViewHolder.getDataBinding();
        Context context = binding.roomDeviceItemConstraintLayout.getContext();
        int imageId = radarBean.getStateImageId();
        binding.roomIvStatus.setImageResource(imageId);
        binding.roomTvDeviceName.setText(StringLengthUtils.getSimpleString(radarBean.getRadarName(), 15));
        binding.roomOffTv.setVisibility(radarBean.getRadarState() == 1 ? View.VISIBLE : View.GONE);
        binding.roomDeviceItemConstraintLayout.setBackground(isUnassigned ?
                context.getDrawable(R.drawable.common_unassigned_bg_normal) : context.getDrawable(R.drawable.room_item_selector));
        binding.roomTvLocation.setVisibility(isUnassigned ? View.GONE : View.VISIBLE);
        binding.roomDevImg.setImageResource(radarBean.getType().getImageRes());
        if (!isUnassigned){
            binding.roomTvLocation.setText(StringLengthUtils.getSimpleString(roomName, 15));
        }
        binding.roomTvUnassignedTag.setVisibility(isUnassigned ? View.VISIBLE : View.GONE);

        binding.roomIvCheck.setVisibility(isShowCheckBox ? View.VISIBLE : View.GONE);
        if (isShowCheckBox){
            setImageChecked(binding, isSelectAll);
            if (isSelectAll){
                checkedItems.put(getItemPosition(radarBean), radarBean);
            }else {
                checkedItems.clear();
            }
            updateSelectCount();

            binding.roomIvCheck.setTag(false);
            binding.roomIvCheck.setOnClickListener(v -> {
                Boolean tag = (Boolean) binding.roomIvCheck.getTag();
                setImageChecked(binding, !tag);
                if (!tag){
                    checkedItems.put(getItemPosition(radarBean), radarBean);
                }else {
                    checkedItems.remove(getItemPosition(radarBean));
                }
                updateSelectCount();
                binding.roomIvCheck.setTag(!tag);
            });

            binding.roomDeviceItemConstraintLayout.setOnClickListener(v -> {
                binding.roomIvCheck.performClick();
            });
        }else {
            if (isUnassigned){
                binding.roomDeviceItemConstraintLayout.setOnClickListener(null);
            }
        }
    }

    //是否选中
    private void setImageChecked(RoomLayoutDeviceItemBinding binding, boolean tag){
        binding.roomIvCheck.setImageResource(tag ? R.drawable.common_select : R.drawable.common_unselected);
    }

    /** 是否显示选择框 */
    public void setShowCheckBox(boolean showCheckBox) {
        isShowCheckBox = showCheckBox;
        notifyDataSetChanged();
    }

    /** 是否全选 */
    public void setSelectAll(boolean selectAll) {
        isSelectAll = selectAll;
        notifyDataSetChanged();
    }

    public boolean isSelectAll() {
        return isSelectAll;
    }

    /** 获取已选项 */
    public Map<Integer, RadarBean> getCheckedItems() {
        return checkedItems;
    }

    public void updateSelectCount(){
        if (onSelect != null){
            onSelect.select(checkedItems.size());
        }
    }

    public void setOnSelect(OnSelect onSelect) {
        this.onSelect = onSelect;
    }

    public interface OnSelect{
        void select(int count);
    }
}
